/**
 * Created by Vivienfanghua on 2016/2/24.
 */
function pieChart(rootname,name,data,cx,cy,r,colors)
{
    var svg = Snap(rootname);
    var g = svg.paper.g();
    g.attr({id:name})
    var total=0;
    data=data.slice(2);
    data.forEach(function(value){total+=value;});
    var angles=data.map(function(x){return x/total*Math.PI*2;});
    var startangle=0;
    for(var i=0;i<data.length;i++)
    {
        var endangle=startangle+angles[i];
        var x1=cx+r*Math.sin(startangle);
        var y1=cy-r*Math.cos(startangle);
        var x2=cx+r*Math.sin(endangle);
        var y2=cy-r*Math.cos(endangle);
        var big=0;
        if(endangle-startangle>Math.PI)
            big=1;
        var d="M "+cx+","+cy+" L "+x1+","+y1+","+" A "+r+","+r+" 0 "+big+" 1 "+x2+","+y2+" Z";
        var path=svg.path(d).attr({fill:colors[i],stroke:"black",strokeWidth:"2"});
        g.add(path);
        startangle=endangle;
    }

    return g;
}

function pieLegend(rootname,name,cx,cy,r,colors,labels,legendName,fontsize,datalength)
{
    var svg = Snap(rootname);
    var g = svg.paper.g();
    g.attr({id:name});
    //var rect=svg.rect(cx-2.2*width,cy-2*height,5*r,4*r,10);
    //rect.attr({fill:"#f6f6f6",stroke:"black",strokeWidth:"2"});
    //g.add(rect);
    var total=0;
    data=[];
    for(var i=0;i<datalength;i++)
        data.push(1);
    data.forEach(function(value){total+=value;});
    var angles=data.map(function(x){return x/total*Math.PI*2;});
    var startangle=0;
    for(var i=0;i<data.length;i++)
    {
        var endangle=startangle+angles[i];
        var x1=cx+r*Math.sin(startangle);
        var y1=cy-r*Math.cos(startangle);
        var x2=cx+r*Math.sin(endangle);
        var y2=cy-r*Math.cos(endangle);
        var x3=cx+r*Math.sin((startangle+endangle)/2);
        var y3=cx-r*Math.cos((startangle+endangle)/2);
        if(x3<cx)
            x3=x3-1.5*r;
        var big=0;
        if(endangle-startangle>Math.PI)
            big=1;
        var d="M "+cx+","+cy+" L "+x1+","+y1+","+" A "+r+","+r+" 0 "+big+" 1 "+x2+","+y2+" Z";
        var path=svg.path(d).attr({fill:colors[i],stroke:"black",strokeWidth:"2"});
        g.add(path);
        startangle=endangle;
        var colorrect=svg.rect(cx+1.1*r,cy-r+1.5*r*i/data.length,0.2*r,0.2*r,0);
        var colorrectheight=colorrect.getBBox().height;
        colorrect.attr({fill:colors[i],stroke:"black",strokeWidth:"2"});
        var textone=svg.text(cx+1.4*r,cy-r+1.5*r*i/data.length+colorrectheight,labels[i]);
        //var text=svg.text(x3,y3,labels[i]);
        textone.attr({"font-weight":"normal","font-size":fontsize+"px","font-family":"Adobe 仿宋 Std R"});
        //text.drag();
        g.add(textone);
        g.add(colorrect);
    }
    var text2=svg.text(cx,cy,legendName);
    text2.attr({"font-weight":"bold","font-size":1.5*fontsize+"px","font-family":"Adobe 仿宋 Std R"});
    var namewidth=text2.getBBox().width;
    var nameheight=text2.getBBox().height;
    text2.attr({x:cx-0.5*namewidth,y:cy-r-0.5*nameheight});
    g.add(text2);
    //svg.append(g);
    return g;
}

//id为要着色的对象，color为color.js中的颜色
function setcolor(id,color)
{
    var colors=color.split(",");
    Snap("#"+id).attr({fill:"#"+parseInt(colors[0]).toString(16)+parseInt(colors[1]).toString(16)+parseInt(colors[2]).toString(16)});
}

function setcolor2(id,color)
{
    var colors=color.split(",");
    var root=document.getElementById(id);
    var childrens=root.children;
    for(var i=0;i<childrens.length;i++)
        childrens[i].setAttribute("fill","#"+parseInt(colors[0]).toString(16)+parseInt(colors[1]).toString(16)+parseInt(colors[2]).toString(16));
}

//分级底色
//Input:数组元素,分组后的类型,色带
function ColorMap(Data,type,colorband)
{

}

//条形图
function bandChart(rootname,name,data,cx,cy,heightPer,widthPer,colors)
{
    var svg = Snap(rootname);
    var g = svg.paper.g();
    g.attr({id:name});
    data=data.slice(2);
    for(var i=0;i<data.length;i++)
    {
        var xone=parseInt(cx+widthPer*i);
        var heightone=data[i]*heightPer;
        var yone=parseInt(cy-heightone);
        var rect=svg.rect(xone,yone,widthPer,heightone);
        rect.attr({fill:colors[i],stroke:"black",strokeWidth:"2"});
        g.add(rect);
    }
    return g;
}

//条形图图例
function bandLegend(rootname,name,labels,number,cx,cy,heightPer,widthPer,strokeWidth,maxValue,colors,fontsize)
{
    var svg = Snap(rootname);
    var g = svg.paper.g();
    g.attr({id:name});
    var nearBigValue=Math.pow(10,Math.log10(maxValue).toFixed(0));
    var maxHeight=parseInt(heightPer*nearBigValue*0.51);
    var maxWidth=parseInt(2*number*widthPer+widthPer);
    var p1 = svg.polyline([cx, cy-maxHeight,cx, cy]).attr({
        stroke: "#000",
        strokeWidth: strokeWidth
    });
    g.add(p1);
    var p11 = svg.polyline([cx, cy,cx+maxWidth,cy]).attr({
        stroke: "#000",
        strokeWidth: strokeWidth
    });
    g.add(p11);
    for(var i=1;i<=5;i++)
    {
        var oney=parseInt(cy-nearBigValue*heightPer*0.1*i);
        var p2=svg.polyline(cx, oney,cx+widthPer*0.2, oney).attr({
            stroke: "#000",
            strokeWidth: parseInt(strokeWidth*0.5)
        });
        g.add(p2);
        var textone=svg.text(cx, oney,""+i).attr({
            "font-weight":"normal","font-size":fontsize+"px","font-family":"宋体"
        });
        var namewidth=textone.getBBox().width;
        textone.attr({x:parseInt(cx-namewidth*1.1)});
        g.add(textone);
    }
    var svgunit=svg.text(cx,cy-maxHeight,""+nearBigValue*0.1).attr({
        "font-weight":"normal","font-size":fontsize+"px","font-family":"宋体"
    });
    g.add(svgunit);
    for(var i=0;i<number;i++)
    {
        var xone=parseInt(cx+(2*i+1)*widthPer);
        var heightone=parseInt(Math.random()*maxHeight);
        var yone=cy-heightone;
        var rectone=svg.rect(xone,yone,widthPer,heightone).attr({
            fill:colors[i],stroke:"black",strokeWidth:"2"
        });
        g.add(rectone);
        var labelone=svg.text(xone,cy,labels[i]).attr({
            "font-weight":"normal","font-size":fontsize+"px","font-family":"宋体"
        });
        var labenoneheight=labelone.getBBox().height;
        labelone.attr({
            cy:cy+labenoneheight
        })
        labelone.transform(new Snap.Matrix().rotate(45,xone,cy+labenoneheight));
        g.add(labelone);
    }
    return g;
}

function Rating(Data,ranknum,type)
{
    var result;
    var returnvalue={};
    var maxvalue=Math.max.apply(null, Data);
    var minvalue=Math.min.apply(null, Data);
    returnvalue["min"]=minvalue;
    returnvalue["max"]=maxvalue;
    switch (type)
    {
        case "equalDiff":
            result=equalDiff(Data,ranknum);
            break;
        case "equalNum":
            result=equalNum(Data,ranknum);
            result=unique(result);
            break;
    }
    returnvalue["type"]=Data.map(function(d){
        var one=-1;
        for(var i=0;i<result.length-1;i++)
            if(d>=result[i]&&d<result[i+1])
            {
                one=i;
                break;
            }
        if(one==-1)
            one=result.length-1;
        return one;
    });
    returnvalue["value"]=result;
    return returnvalue;
}

//去除重复元素，针对等数量分级可能造成数值相等的情况
function unique(array)
{
    var result=[];
    array.forEach(function(d){
       if(result.indexOf(d)==-1)
        result.push(d);
    });
    return result;
}

//等差分级
//包括下界不包括上界
function equalDiff(Data,ranknum)
{
    var result=[];
    var maxvalue=Math.max.apply(null, Data);
    var minvalue=Math.min.apply(null, Data);
    var pervalue=(maxvalue-minvalue)/(ranknum-1);
    for(var i=0;i<ranknum;i++)
        result.push(minvalue+i*pervalue);
    return result;
}

//等数量分级
//包括下界不包括上界
function equalNum(Data,ranknum)
{
    var result=[];
    var sortData=Data.slice(0).sort();
    var pernum=parseInt(Data.length/ranknum);
    for(var i=0;i<ranknum;i++)
        result.push(sortData[pernum*i]);
    return result;
}

function Legend_C_Pie(rootname,name,width,height,cx,cy,minsize,maxsize,valuearray,fontsize)
{
    var dataset={startAngle:0,endAngle:Math.PI};
    var sizearray=getDData(minsize,maxsize,valuearray,4);
}

//连续比率
//Input:最小尺寸，最大尺寸，数组
//Output:尺寸数组
function getCData(minsize,maxsize,valuesarray)
{
    var maxvalue=Math.max.apply(null, valuesarray);
    var minvalue=Math.min.apply(null, valuesarray);
    var valuerange=maxvalue-minvalue;
    var sizerange=maxsize-minsize;
    var returnvalue=[];
    return valuesarray.map(function(d){
        return (d-minvalue)/valuerange*sizerange+minsize;
    })
}

//间隔比率
//Input:最小尺寸，最大尺寸，数组，间隔数
//Output:尺寸数组
function getDData(minsize,maxsize,valuesarray,internum)
{
    var num=internum-1;
    var maxvalue=Math.max.apply(null, valuesarray);
    var minvalue=Math.min.apply(null, valuesarray);
    var valuerange=maxvalue-minvalue;
    var pervalue=valuerange/num;
    var sizerange=maxsize-minsize;
    var persize=sizerange/num;
    var returnvalue=[];
    return valuesarray.map(function(d){
        return parseInt(1.0*(d-minvalue)/pervalue)*persize+minsize;
    })
}